import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/homeworks.dart';
import 'package:ichinesereader/models/teacher/AssignmentType.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/homeworkDatagrid.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';

class HomeworkManagement extends StatefulWidget {
  static const String routeName = '/teacher/homework';

  @override
  State<HomeworkManagement> createState() => _HomeworkManagementState();
}

class _HomeworkManagementState extends State<HomeworkManagement> {
  final box = Hive.box('appconfig');
  final HomeworksController controller = Get.find();
  bool isTeacher = false;

  @override
  void initState() {
    isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    var classId = box.get(CacheManagerKey['currentclass'].toString()) ?? '';

    if (isTeacher) {
      AssignmentType _assignmentType = AssignmentType();
      _assignmentType.assignmentType = 'ASSIGNMENT';
      _assignmentType.live = true;
      _assignmentType.queued = true;
      _assignmentType.past = false;

      if (classId != '' && classId != null) {
        Future.delayed(Duration(milliseconds: 100), (() {
          controller.fetchTeacherHomworks(classId, _assignmentType);
        }));
      }
    }
    super.initState();
  }

  @override
  void dispose() {
    controller.assignmentItems.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher homeworks Management');

    return Scaffold(
      backgroundColor: Colors.transparent,
      //floatingActionButton: FloatingActionButtonsTeacherSwitchClass(),
      body: Column(
        children: [
          isTeacher
              ? Expanded(
                  child: controller.obx(
                  (data) => HomeworkDataGrids(
                    true,
                    controller.assignmentItems,
                  ),
                  onEmpty: HomeworkDataGrids(
                    true,
                    controller.assignmentItems,
                  ),
                  onLoading: Container(),
                ))
              : Text(''),
        ],
      ),
      // Container(
      //   decoration: _pageStyles.pageBoxDecoration(),
      //   child: Row(
      //     children: [
      //       Visibility(
      //           visible: !isSmallScreen,
      //           child: TeacherSidebarX(
      //             selectedIndex: _selectedSidebarItem,
      //             isBigScreen: isBigScreen,
      //           )),
      //       Expanded(
      //         child:
      //         Column(
      //           children: [
      //             isTeacher
      //                 ? Expanded(
      //                     child: controller.obx(
      //                     (data) => HomeworkDataGrids(
      //                       true,
      //                       controller.assignmentItems,
      //                     ),
      //                     onEmpty: HomeworkDataGrids(
      //                       true,
      //                       controller.assignmentItems,
      //                     ),
      //                     onLoading: Container(),
      //                   ))
      //                 : Text(''),
      //           ],
      //         ),
      //       ),
      //     ],
      //   ),
      // ),
    );
  }
}
